package designPattern.action11.Visitor.Q1.concrete.visitor;

import java.util.Iterator;

import designPattern.action11.Visitor.Q1.concrete.entry.DirectoryEntry;
import designPattern.action11.Visitor.Q1.concrete.entry.FileEntry;
import designPattern.action11.Visitor.Q1.sketch.Entry;
import designPattern.action11.Visitor.Q1.sketch.Visitor;

public class ListVisitor extends Visitor {
    private String currentdir = "";                         // 当前访问的文件夹的名字

    public void visit(FileEntry fileEntry) {                  // 在访问文件时被调用
        System.out.println(currentdir + "/" + fileEntry);
    }

    public void visit(DirectoryEntry directory) {   // 在访问文件夹时被调用
        System.out.println(currentdir + "/" + directory);
        String savedir = currentdir;
        currentdir = currentdir + "/" + directory.getName();
        Iterator it = directory.iterator();
        while (it.hasNext()) {
            Entry entry = (Entry) it.next();
            entry.accept(this);
        }
        currentdir = savedir;
    }
}
